package com.bluefuture.modules.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.bluefuture.common.utils.OSSUtil;
import com.bluefuture.common.utils.ResultVo;
import com.bluefuture.common.utils.ResultVoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @Author: leo
 * @date: 2019/9/9
 */
@RestController
@RequestMapping
@Api(tags = "文件接口")
public class FileController {

    /**
     * 图片上传
     */
    @PostMapping("file/img")
    @ApiOperation(value = "图片上传接口")
    public ResultVo imgUpload(@RequestParam("file") MultipartFile file) throws IOException {
        //获取上传原图片名称
        String fileName = file.getOriginalFilename();
        //生成保存在服务器的图片名称，延用原后缀名
        String newFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
        OSSUtil.upload(newFileName, file.getInputStream());
        JSONObject result = new JSONObject();
        result.put("url", OSSUtil.getUrl(newFileName, ""));
        return ResultVoUtil.success(result);
    }

}
